//
//  NetworkLostHUD.m
//  Onskysell
//
//  Created by LJJ on 2017/8/21.
//  Copyright © 2017年 cg. All rights reserved.
//

#import "NetworkLostHUD.h"

@interface NetworkLostHUD ()



@end

@implementation NetworkLostHUD

- (instancetype)initWithDuration:(CGFloat)duration
{
    self = [super init];
    if (self) {
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 230, 110)];
        self.contentView.center = KWindow.center;
        self.contentView.layer.cornerRadius = 5.0f;
        self.contentView.layer.borderWidth = 0.5f;
        self.contentView.layer.borderColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;
        self.contentView.backgroundColor = [UIColor colorWithRed:0.2f
                                                        green:0.2f
                                                         blue:0.2f
                                                        alpha:0.9f];
        self.contentView.tag = 6699;
        [KWindow addSubview:self.contentView];
        
        UIImageView *imgV = UIImageView.new;
        imgV.image = [UIImage imageNamed:@"yb_error"];
        [self.contentView addSubview:imgV];
        [imgV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@8);
            make.centerX.equalTo(self.contentView.mas_centerX);
            make.width.height.equalTo(@30);
        }];
        
        UILabel *label1 = UILabel.new;
        label1.text = @"Network Error";
        label1.textAlignment = NSTextAlignmentCenter;
        label1.textColor = [UIColor whiteColor];
        label1.font = [UIFont boldSystemFontOfSize:14.0f];
        [self.contentView addSubview:label1];
        [label1 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imgV.mas_bottom).offset(8);
            make.centerX.equalTo(self.contentView.mas_centerX);
        }];
        
        UILabel *label2 = UILabel.new;
        label2.text = @"Your device appears to have lost network connectivity";
        label2.textAlignment = NSTextAlignmentCenter;
        label2.textColor = [UIColor whiteColor];
        label2.font = [UIFont systemFontOfSize:13.0f];
        label2.numberOfLines = 0;
        [self.contentView addSubview:label2];
        [label2 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(label1.mas_bottom).offset(5);
            make.centerX.equalTo(self.contentView.mas_centerX);
            make.left.equalTo(self.contentView.mas_left).offset(10);
            make.right.equalTo(self.contentView.mas_right).offset(-10);
        }];
    }
    return self;
}


+ (void)show{
    for (UIView *v in KWindow.subviews) {
        if (v.tag == 6699) {
            [v removeFromSuperview];
        }
    }
    NetworkLostHUD *hud = [[NetworkLostHUD alloc] initWithDuration:3.0];
    [hud showAnimation];
    [hud performSelector:@selector(hideAnimation) withObject:nil afterDelay:3.0];
}

- (void)showAnimation{
    [UIView beginAnimations:@"show" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.3];
    self.contentView.alpha = 1.0f;
    [UIView commitAnimations];
}

- (void)hideAnimation{
    [UIView beginAnimations:@"hide" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(remove)];
    [UIView setAnimationDuration:0.3];
    self.contentView.alpha = 0.0f;
    [UIView commitAnimations];
}

- (void)remove{
    [self.contentView removeFromSuperview];
}
@end
